/**
 * Copyright (c) 2013-Now http://aidex.vip All rights reserved.
 */
// 此处第二个参数vm，就是我们在页面使用的this，你可以通过vm获取vuex等操作
const install = (Vue, vm) => {
	// 通用请求头设定
	const ajaxHeader = 'x-ajax';
	const sessionIdHeader = 'Authorization';
	const rememberMeHeader = 'x-remember';
	// const token = vm.vuex_token ?vm.vuex_token :''
	const authorization = vm.vuex_authorization ? vm.vuex_authorization :''
	//this.baseUrl = uni.getStorageSync("baseUrl")\
	let url = "http://"+uni.getStorageSync("baseUrl")
	// let url = vm.vuex_config.baseUrl
	console.log("http.interceptor.js的url=>"+url)
	console.log("http.interceptor.js的authorization=>"+authorization)
	// 请求参数默认配置
	Vue.prototype.$u.http.setConfig({
		baseUrl: url,
		// baseUrl: vm.vuex_config.baseUrl,
		originalData: true, 
		// 默认头部，http2约定header名称统一小写 aidex
		header: { 
			// 'content-type': 'application/json;application/json;charset=utf-8',
			"Access-Control-Allow-Origin":"*",
			'authorization': authorization,
			// 'token':token ,
			// config.headers["Access-Control-Allow-Origin"]="http://127.0.0.1:8080";
			// 'content-type': 'application/x-www-form-urlencoded',
			// 'x-requested-with': 'XMLHttpRequest'
		},
		
	});
	
	// 请求拦截，配置Token等参数
	Vue.prototype.$u.http.interceptor.request = (req) => {
		if (!req.header){
			req.header = [];
		}
		req.header["source"] = "app";
		
		// 默认指定返回 JSON 数据
		if (!req.header[ajaxHeader]){
			req.header[ajaxHeader] = 'json';
		}
		
		// 设定传递 Token 认证参数 aidex
		if (!req.header[sessionIdHeader] && vm.vuex_token){
			req.header[sessionIdHeader] = vm.vuex_token;
		}
	
		
		// 为节省流量，记住我数据不是每次都发送的，当会话失效后，尝试重试登录 aidex
		if (!req.header[rememberMeHeader] && vm.vuex_remember && req.remember){
			req.header[rememberMeHeader] = vm.vuex_remember;
			req.remember = false;
		}
		
		
		console.log('request.url==>'+url);
		// post的参数,部分方法拼接起来传递的方式进行传递,
		if(req.data && req.method == 'POST'){
			let ret = '?'
			        for (const it in req.data) {
			          ret +=
			            encodeURIComponent(it) +
			            '=' +
			            encodeURIComponent(req.data[it]) +
			            '&'
			        }
			req.url=req.url+ret.slice(0, -1);
		}
		// const a = {
		// 	"method":"GET",
		// 	"url":"/wms/PDA/pageDSInStorageForPDA",
		// 	"header":{
		// 		"source":"app",
		// 		"x-ajax":"json",
		// 		"Authorization":"Bearer 4278f705-8a62-4f28-9328-9e97bf7febdf",
		// 		},
		// 	"data":{
		// 		"size":10,
		// 		},
		// 	}
	    console.log('request==>'+JSON.stringify(req));
		return req;
	}
	
	// 响应拦截，判断状态码是否通过
	Vue.prototype.$u.http.interceptor.response = async (res, req) => {

	    console.log('response<=='+JSON.stringify(req));
		let data = res.data;
		if (!(data)){
			// vm.$u.toast('未连接到服务器,请重启后查看后台服务和端口号')
			return false;
		}
		
		if (typeof data === 'object' && !(data instanceof Array)){
			if (data.token){
				vm.$u.vuex('vuex_token', data.token);
				if (data.user){
					vm.$u.vuex('vuex_user', data.user);
				}
				vm.$u.vuex('vuex_isAgent', data.isAgent);
			}
			if (data.result === 'login'){
				vm.$u.vuex('vuex_user', {});
				if (req.tryagain == undefined || req.tryagain){
					req.tryagain = false; req.remember = true;
					await vm.$u.http.request(req).then(res => {
						data = res;
					});
				}
				if (data.result === 'login'){
					if (!req.data.loginCheck){
						vm.$u.toast(data.msg);
					}
					req.tryagain = true;
				}
			}
		}
		
		if (res.header && res.header[rememberMeHeader]){
			let remember = res.header[rememberMeHeader];
			if (remember && remember != 'deleteMe'){
				vm.$u.vuex('vuex_remember', remember);
			}else{
				vm.$u.vuex('vuex_remember', '');
			}
		}
		let datasource=uni.getStorageSync('datasource') ? 'oracle' : 'oracle'
		// console.log('http.interceptor.js: datasource==>'+datasource)
		// if(datasource==='oracle'){
		// 	if(data.data){
		// 		data.data = JSON.parse(data.data)
		// 	}
		// }
		//let data = JSON.parse(res.data)
		// oracle
		// data.data = JSON.stringify(data.data)
		// mysql就吧上一行隐藏掉就行
		return data;
	}
	
	// 封装 get text 请求
	vm.$u.getText = (url, data = {}, header = {}) => {
		return vm.$u.http.request({
			dataType: 'text',
			method: 'GET',
			url,
			header,
			data
		})
	}
	
	// 封装 post json 请求
	vm.$u.postJson = (url, data = {}, header = {}) => {
		// header['content-type'] = 'application/json';
		// header['content-type'] = 'multipart/form-data';
		// transformRequest: [
		//       function(data) {
		//         let ret = ''
		//         for (const it in data) {
		//           ret +=
		//             encodeURIComponent(it) +
		//             '=' +
		//             encodeURIComponent(data[it]) +
		//             '&'
		//         }
		//         return ret
		//       }
		//     ];
		return vm.$u.http.request({
			url,
			method: 'POST',
			header,
			data:transformRequest
		})
	}
	
	
	
	
	
	
	
}

export default {
	install
}